package com.ginage.gaugemanager.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ginage.gaugemanager.mapper.entity.User;
import com.ginage.gaugemanager.service.api.IUserCenterService;


@Controller
public class UserCenterController {
	@Autowired
	IUserCenterService service;
	
	@RequestMapping("uploadTouxiang.do")
	public void uploadTouxiang(@RequestParam MultipartFile img,HttpServletRequest request,HttpServletResponse response) {
		HttpSession session=request.getSession(false);
		if(session==null) {
			return;
		}
		User currentUser=(User) session.getAttribute("currentUser");
		String fileName=img.getOriginalFilename();
		//String path=session.getServletContext().getRealPath("/images")+"/"+currentUser.getUsername()+"/touxiang";
		//sk3800\AppData\Local\Temp\tomcat-docbase.7809082671058090294.8900\images\lsk3800\touxiang\图标.jpg
		String path="/src/main/resources/static/images/"+currentUser.getUsername()+"/touxiang/";
		String userDir = System.getProperty("user.dir");
		File file=new File(userDir+path);
		if(!file.exists()) {
			file.mkdirs();
		}
		file=new File(userDir+path,fileName);
		try {
			img.transferTo(file);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONObject jb = new JSONObject();
		jb.put("status","success");
		jb.put("path", file.getPath());
		try {
			response.getWriter().print(jb);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String touxiangPath="images/"+currentUser.getUsername()+"/touxiang/"+fileName;
		service.saveTouxiangPath(touxiangPath,currentUser.getUsername());
	}
	@RequestMapping("userCenter.html")
	public String userCenterHtml() {
		return "userCenter.html";
	}
	
	@RequestMapping("getUserInfo.do")
	
	
	public void getUserInfo(HttpServletRequest request,HttpServletResponse response) {
		HttpSession session=request.getSession();
		User currentUser=(User) session.getAttribute("currentUser");
		JSONObject jb=service.getUserInfo(currentUser.getUsername());
		if(jb.get("status")=="success") {
			try {
				response.getWriter().print(jb);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	@RequestMapping("getAllUsers.do")
	
	public void getAllUsers(HttpServletRequest request,HttpServletResponse response) {
		int pageNum=Integer.parseInt(request.getParameter("pageNum"));
		JSONArray ja=new JSONArray();
		ja=service.getUsers(pageNum);
		try {
			response.getWriter().print(ja);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
